<?php

/**
 * This is the model class for table "quiz".
 *
 * The followings are the available columns in table 'quiz':
 * @property integer $id
 * @property integer $category_id
 * @property string $title
 * @property string $description
 * @property string $image
 * @property integer $no_of_level
 * @property integer $no_of_questions
 * @property integer $passing_percentage
 * @property string $status
 * @property string $created
 * @property string $modified
 */
class Quiz extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Quiz the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'quiz';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('category_id, title, description, no_of_level, no_of_questions, passing_percentage, status,time', 'required'),
			array('category_id, no_of_level, no_of_questions, passing_percentage', 'numerical', 'integerOnly'=>true),
			array('title', 'length', 'max'=>200),
			array('image', 'length', 'max'=>500),
			array('status', 'length', 'max'=>9),
			array('time', 'length', 'max'=>50),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, category_id, title, description, image, no_of_level, no_of_questions, passing_percentage, status,time, created, modified', 'safe', 'on'=>'search'),
			array('modified','default',
           	  'value'=>new CDbExpression('NOW()'),
              'setOnEmpty'=>false,'on'=>'update'),
        	array('created,modified','default',
              'value'=>new CDbExpression('NOW()'),
              'setOnEmpty'=>false,'on'=>'insert'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}
 	
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'category_id' => 'Category',
			'title' => 'Quiz Name',
			'description' => 'Description',
			'image' => 'Image',
			'no_of_level' => 'No Of Level',
			'no_of_questions' => 'No Of Questions',
			'passing_percentage' => 'Passing Percentage',
			'status' => 'Status', 
      'user_id'=>'Author',
			'time'=>'Time Limit(min)',
			'created' => 'Created',
			'modified' => 'Modified',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
		$objcat='';	
		if (isset($this->category_id) && $this->category_id != null)
		{
		$objcat=Category::model()->find('name=?',array($this->category_id));
		}
		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		
		if(!empty($objcat))
		$criteria->compare('category_id',$objcat->id);
		else
		$criteria->compare('category_id',$this->category_id);
		
		
		$criteria->compare('title',$this->title,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('image',$this->image,true);
		$criteria->compare('no_of_level',$this->no_of_level);
		$criteria->compare('no_of_questions',$this->no_of_questions);
		$criteria->compare('passing_percentage',$this->passing_percentage);
		if($_SESSION['role_id'] !="1")
		$criteria->compare('user_id',Yii::app()->user->id);
		$criteria->compare('status',$this->status,true);
		$criteria->compare('time',$this->status,true);
		$criteria->compare('created',$this->created,true);
		$criteria->compare('modified',$this->modified,true);
    $criteria->order = 'created DESC';
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
  public static function getCriteria($type, $arrParams)
	{
		switch($type)
		{
						
		case "BY-SEARCH":
			$condition=null;
			$sort="title";
			$criteria = new CDbCriteria;
			//if(!empty($arrParams['loc_type']) && $arrParams['loc_type'] != "")
					
			if(!empty($arrParams['type']))
	    	{
	    		$search_type=$arrParams['type'];
	    		
	    		if ($search_type=="quiz")
	    		{
	    			$arrParams['quiz'] = str_replace("'","\'",$arrParams['quiz']);
	    			if($condition == "")
	    				$condition="quiz.title LIKE '%".$arrParams['quiz']."%'";
	    			else 
	    				$condition=$condition." AND quiz.title LIKE '%".$arrParams['quiz']."%'";	
	    		}
	    	}
	    	
		
	}
}
public static function getQuestionTotalPoints($quizid,$attemptid)
	{
		$userattemptdetails=UserQuizAttemptDetails::getList("ATTEMPT-ID",array('attemptid'=>$attemptid));
		$sum=0;
		foreach ($userattemptdetails as $detail)
		{
			$questiondetail=Questions::model()->getModel("QUESTION-ID", array('questionid'=>$detail->question_id));
			$sum=$sum+(int)$questiondetail->points;
		}
		return $sum;
		
	}
public function getModel($type, $arrParams) 
	{
		switch($type)
		{
			
			case "CHAPTER-ID":
				$chapter=Chapter::model()->findByPk((int)$arrParams['chapterid']);
				return $chapter;
			break;
			
			case "QUIZ-ID":
				$quiz=Quiz::model()->findByPk((int)$arrParams['quizid']);
				return $quiz;
			break;
			
			case "QUIZ-SEARCH":
				$quiz=Quiz::model()->find('title=?',array($arrParams['q']));
				return $quiz;
			break;
	
		
		}
	}
	public static function getList($type, $arrParams) 
	{
		switch($type)
		{
						
			case "QUIZ-LIST":
				if($_SESSION['role_id'] !="1")
				$quiz=Quiz::model()->findAll('user_id=?',array(Yii::app()->user->id));
				else
					$quiz=Quiz::model()->findAll();
				return $quiz;
			break;
	
		
		}
	}
}
